﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DonutLandLibrary.Robots;
using DonutLandLibrary.Robots.Environments;
using DonutLandLibrary.Robots.GeneticAlgorithm;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        RobotEnvironment robotEnvironment;

        public Form1()
        {
            InitializeComponent();

            LoadPreviousEnvironmentsIDs();


            ////max possible genetic profile number
            //string bitStream = "1000100010001100101000110010";
            ////"110010"

            //GeneticProfile geneProfile = new GeneticProfile(bitStream);

            //string output = geneProfile.ToString();
        }


        private void LoadPreviousEnvironmentsIDs()
        {
            List<int> environments = RobotEnvironment.GetEnvironments();

            if (environments.Count > 0)
            {
                cbPreviousEnvironments.DataSource = environments;
            }
        }

        private void DisplayStats()
        {
            if (robotEnvironment != null)
            {
                lblPower.Text = "Power Sources : " + robotEnvironment.Squares.PowerSources.ToString();

                lblCycle.Text = "Cycle : " + robotEnvironment.CycleCount.ToString();
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            robotEnvironment = new RobotEnvironment();

            robotEnvironment.Save();

            LoadPreviousEnvironmentsIDs();

            dgEnvironment.DataSource = (DataTable)robotEnvironment.Squares.renderSquareCollection(Square.renderFormat.data);
            dgEnvironment.AutoGenerateColumns = true;

            DisplayStats();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            int environmentID;

            int.TryParse(cbPreviousEnvironments.SelectedValue.ToString(), out environmentID);

            if (environmentID != null)
            {
                robotEnvironment = RobotEnvironment.Load(environmentID);

                dgEnvironment.DataSource = (DataTable)robotEnvironment.Squares.renderSquareCollection(Square.renderFormat.data);
                dgEnvironment.AutoGenerateColumns = true;

                dgRobots.DataSource = robotEnvironment.RobotCollection;
                dgRobots.AutoGenerateColumns = true;

                DisplayStats();
            } 
        }

        private void btnSpawn_Click(object sender, EventArgs e)
        {
            if (robotEnvironment != null)
            {
                int count = 0;

                while (count < 100)
                {
                    robotEnvironment.SpawnRobot();

                    count++;
                }

                dgEnvironment.DataSource = null;
                dgEnvironment.DataSource = (DataTable)robotEnvironment.Squares.renderSquareCollection(Square.renderFormat.data);
                dgEnvironment.AutoGenerateColumns = true;

                dgRobots.DataSource = null;
                dgRobots.DataSource = robotEnvironment.RobotCollection;
                dgRobots.AutoGenerateColumns = true;

                DisplayStats();
            }
        }

        private void btnCycle_Click(object sender, EventArgs e)
        {
            if (robotEnvironment != null)
            {
                int count = 0;

                //while (count < 99)
                while (robotEnvironment.Squares.PowerSources > 0)
                {
                    robotEnvironment.Cycle();

                    count++;
                }

                dgEnvironment.DataSource = (DataTable)robotEnvironment.Squares.renderSquareCollection(Square.renderFormat.data);
                dgEnvironment.AutoGenerateColumns = true;

                dgRobots.DataSource = null;
                dgRobots.DataSource = robotEnvironment.RobotCollection;
                dgRobots.AutoGenerateColumns = true;

                DisplayStats();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            robotEnvironment.Save();
        }

        private void btnStep_Click(object sender, EventArgs e)
        {
            if (robotEnvironment != null)
            {
                robotEnvironment.Cycle();

                dgEnvironment.DataSource = (DataTable)robotEnvironment.Squares.renderSquareCollection(Square.renderFormat.data);
                dgEnvironment.AutoGenerateColumns = true;

                dgRobots.DataSource = null;
                dgRobots.DataSource = robotEnvironment.RobotCollection;
                dgRobots.AutoGenerateColumns = true;

                DisplayStats();
            }
        }
    }
}
